package com.aube.plugin.figureex;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.figure.FigureView;
import com.aube.plugin.model.CharacterEx;
import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.model.CategorySimple;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.Utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by hill on 17/1/3.
 */

public class FigureExWidget extends DLLayout {
    private final String TAG = "Aube/FigureExWidget";

    private Context mContext;

    private LinearLayout mLayoutGroup;
    private View mFlag, mHeadLayout;
    private TextView mTitle;
    private CategorySimple mCate;
    private IPositionManager mManager;
    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private FigureExGroupView mFigureExGpView;

    public FigureExWidget(Context context) {
        this(context, null);
    }
    public FigureExWidget(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public FigureExWidget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        initViews();
    }

    @Override
    public void initViews() {
        inflate(R.layout.layout_figureex, this, true);
        mLayoutGroup = (LinearLayout) findViewById(R.id.figureex_actor_group);

        mFlag = findViewById(R.id.figureex_header_flag);
        mTitle = (TextView) findViewById(R.id.figureex_header_title);
        mHeadLayout = findViewById(R.id.figureex_header_widget);

        mFlag.setAlpha(0f);
        mTitle.setAlpha(0f);
    }

    @Override
    public void fillData(Object object) {
        if(object == null || !(object instanceof Category))
            return;
        Category category = (Category) object;
        mCate = category.getSimpleCategory();

        mTitle.setText(category.modelName);
        if(StringUtils.isNotBlank(category.modelDesc)) {
            mHeadLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBridge != null) {
//                        mBridge.onClickToList(mCate);
                    }
                }
            });
        }

        List list = (List) category.dataDetail;
        List<CharacterEx> characters = new ArrayList<>();
        CharacterEx cItem;
        for(Object obj : list) {
            cItem = new CharacterEx();
            LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
            Iterator<String> keyIterator = lhm.keySet().iterator();
            while (keyIterator.hasNext()) {
                String key = keyIterator.next();
                Object value = lhm.get(key);

                if(Character.ACTOR_NAME.equalsIgnoreCase(key)) {
                    cItem.actorName = (String) value;
                } else if(Character.ACTOR_PIC.equalsIgnoreCase(key)) {
                    cItem.actorPic = (String) value;
                } else if("actorDesc".equalsIgnoreCase(key)) {
                    cItem.actorDesc = (String) value;
                } else if("guestName".equalsIgnoreCase(key)) {
                    cItem.actorName = (String) value;
                } else if("guestPic".equalsIgnoreCase(key)) {
                    cItem.actorPic = (String) value;
                } else if(Character.PRAISE_COUNT.equalsIgnoreCase(key)) {
                    cItem.praiseCount = (int) ((double) value);
                } else if(Character.RELATED_ID.equalsIgnoreCase(key)) {
                    cItem.relatedId = (String) value;
                } else if("guestDesc".equalsIgnoreCase(key)) {
                    cItem.actorDesc = (String) value;
                } else if("praised".equalsIgnoreCase(key)) {
                    cItem.praised = (String) value;
                } else if("period".equalsIgnoreCase(key)) {
                    cItem.period = (String) value;
                } else if("isNotClick".equalsIgnoreCase(key)) {
                    cItem.isNotClick = (String) value;
                } else if("talentList".equalsIgnoreCase(key)) {
                    List<Character> talents = (List<Character>) value;
                    cItem.talentList = talents;
                }
            }
            characters.add(cItem);
        }

        mFigureExGpView = new FigureExGroupView(mContext);
        mFigureExGpView.fillData(mBridge, characters);
        mLayoutGroup.addView(mFigureExGpView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    @Override
    public void notifyAnim(int animDelay) {
        if(mManager == null)
            return;
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        mLayoutGroup.setVisibility(View.VISIBLE);
        showHeader(animDelay);

        if(mFigureExGpView != null)
            mFigureExGpView.animInGridItems(animDelay + 300);
    }

    private void showHeader(int animDelay) {
        ObjectAnimator alphaTitle = ObjectAnimator.ofFloat(mTitle, "alpha", 0, 1);
        alphaTitle.setDuration(300);
        if(animDelay > 0)
            alphaTitle.setStartDelay(animDelay);

        ObjectAnimator translate = ObjectAnimator.ofFloat(mFlag, "translationY", -mFlag.getHeight(), 0);
        translate.setDuration(300);
        translate.setStartDelay(200 + animDelay);
        translate.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mFlag.setAlpha(1f);
            }
        });

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(alphaTitle, translate);
        mSet.start();
    }

    @Override
    public void registToManager(IPositionManager manager) {
        if(mManager != null || manager == null)
            return;
        manager.add(this);
        this.mManager = manager;

        mLayoutGroup.setVisibility(View.INVISIBLE);
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
        if(mManager == null || manager == null)
            return;
        manager.remove(this);
        this.mManager = null;
    }

    @Override
    public int[] getLocations() {
        int[] locs = new int[2];
        getLocationOnScreen(locs);
        try {
            locs[1] = locs[1] + getHeight() / 2;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locs;
    }

    @Override
    public void setTheme(int themeColor) {
        mFlag.setBackgroundColor(themeColor);
    }
}
